package com.tust.controller;

import com.tust.domain.Selection;
import com.tust.service.SelectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class SelectionController {
    private String userName;
    @Autowired
    private SelectionService selectionService;

    @ResponseBody
    @RequestMapping("/selectSelectionAll.do")
    public List<Selection> selectCourseAll(String userName) {
        List<Selection> selections = selectionService.selectSelectionAll();
        this.userName = userName;
        return selections;
    }

    @ResponseBody
    @RequestMapping("/insertSelection.do")
    public String insertSelection(String studentNum, String courseNum,
                                  String electiveYear, String electiveSemester) {
        String msg =null;
        Selection newSelection = new Selection(studentNum, courseNum, electiveYear, electiveSemester);
        List<Selection> selection = selectionService.selectSelection(newSelection);
        if (selection.size() == 1) {//选课存在
            msg = "selectionExist";
        } else {
            if (selectionService.insertSelection(newSelection) == 1) {//插入成功
                msg = "insertSuccess";
            } else {//插入失败
                msg = "insertFalse";
            }
        }
        return msg;
    }

    @ResponseBody
    @RequestMapping("/updateSelection.do")
    public String updateSelection(String oldStudentNum, String oldCourseNum,
                                  String oldElectiveYear, String oldElectiveSemester,
                                  String studentNum, String courseNum,
                                  String electiveYear, String electiveSemester) {
        String msg = null;
        Selection newSelection = new Selection(studentNum, courseNum, electiveYear, electiveSemester);
        List<Selection> selection = selectionService.selectSelection(newSelection);
        if (selection.size() == 1 && (!oldStudentNum.equals(studentNum)
                || !oldCourseNum.equals(courseNum) || !oldElectiveYear.equals(electiveYear)
                || !oldElectiveSemester.equals(electiveSemester))) {//新选课存在
            msg = "newSelectionExist";
        } else {
            Selection oldSelection = new Selection(oldStudentNum, oldCourseNum, oldElectiveYear, oldElectiveSemester);
            if (selectionService.updateSelection(oldSelection, newSelection) == 1) {//修改成功
                msg = "updateSuccess";
            } else {//修改失败
                msg = "updateFalse";
            }
        }
        return msg;
    }

    @ResponseBody
    @RequestMapping("/selectSelection.do")
    public List<Selection> selectSelection(String studentNum, String courseNum,
                                           String electiveYear, String electiveSemester) {
        List<Selection> selection = null;
        if (!studentNum.equals("")) {//查询学号
            selection = selectionService.selectSelectionByStudentNum(studentNum);
        } else if (!courseNum.equals("")) {//查询课程号
            selection = selectionService.selectSelectionByCourseNum(courseNum);
        } else if (!electiveYear.equals("")) {//查询选课学年
            selection = selectionService.selectSelectionByElectiveYear(electiveYear);
        } else if (!electiveSemester.equals("")) {//查询选课学期
            selection = selectionService.selectSelectionByElectiveSemester(electiveSemester);
        }
        return selection;
    }

    @ResponseBody
    @RequestMapping("/deleteSelection.do")
    public String deleteSelection(String studentNum, String courseNum,
                                  String electiveYear, String electiveSemester) {
        String msg = null;
        Selection selection = new Selection(studentNum, courseNum, electiveYear, electiveSemester);
        if (selectionService.deleteSelection(selection) == 1) {//删除成功
            msg = "deleteSuccess";
        } else {//删除失败
            msg = "deleteFalse";
        }
        return msg;
    }

    @RequestMapping("/studentSelectionRefresh.do")
    public ModelAndView studentSelectionRefresh() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("studentSelection");
        mv.addObject("userName", userName);
        mv.addObject("modelName", "学生选课操作");
        mv.addObject("animate","false");
        return mv;
    }
}
